<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------

namespace app\manage\controller;

use app\manage\model\Miniapp as MiniappModel;
use buwang\base\AdminBaseController;
use think\facade\Db;

class Miniapp extends AdminBaseController
{
    /*protected $middleware = [
        'login' => ['except' => []]
    ];*/

    public function index()
    {
        if (request()->isAjax()) {
            $list = MiniappModel::getList();

            $data = compact('list');
            return $this->success($data);
        }

        return view();
    }

    public function install()
    {
        $dir = $this->request->param('dir');
        if (empty($dir)) return $this->error('未找到应用');

        //获取应用信息
        $param = MiniappModel::getMiniappVersion($dir);
        if (empty($param)) return $this->error('未找到应用配置');

        $app = MiniappModel::column('miniapp_dir');
        if (in_array($dir, $app)) return $this->error('应用已安装,禁止重复安装');

        //插入一条数据
        $data = [
            'types' => $param['types'],
            'title' => $param['name'],
            'version' => $param['version'],
            'is_manage' => $param['is_manage'],
            'is_wechat_pay' => $param['is_wechat_pay'],
            'is_alipay_pay' => $param['is_alipay_pay'],
            'is_openapp' => $param['is_openapp'],
            'describe' => $param['describe'],
//                'view_pic'      => Request::root(true)."/static/{$dir}/logo.png",
//                'style_pic'     => [Request::root(true)."/static/{$dir}/view.png"],
            'view_pic' => '',
            'style_pic' => '',
            'content' => $param['describe'],
            'expire_day' => 0,
            'sell_price' => 0,
            'market_price' => 0,
            'miniapp_dir' => $dir,
            'template_id' => 0,
            'qrcode' => ''
        ];

        //参数验证
//            $this->validate($data,'miniapp.add');

        Db::startTrans();
        try {

            return $this->error('未找到数据库脚本');

            $file = base_path($dir) . 'install.sql';
            if (file_exists($file)) {
                /*$array = get_sql_array($file);
                foreach ($array as $sql) {
                    Db::query($sql);
                }
                MiniappModel::create($data);*/

                Db::commit();
            } else {
                Db::rollback();
                return $this->error('未找到数据库脚本');
            }
        } catch (\Exception $e) {
            Db::rollback();
            return $this->error($e->getMessage());
        }

        return $this->success('安装成功');
    }
}
